package ru.adhoc.truealarm.activities;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.Toast;

import ru.adhoc.truealarm.R;
import ru.adhoc.truealarm.time.TimeHelper;
import ru.adhoc.truealarm.utils.Util;

/**
 * Created by Малюгин Сергей on 03.10.13.
 */
public class CancelAlarmClassic extends Activity implements SeekBar.OnSeekBarChangeListener {

    private Context context;
    private int id;
    private static final String KEY_PREFERENCE = "pref";
    private Thread t;
    private boolean continProgress;
    private LinearLayout llParentClacssic;

    private SeekBar seekBar;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.cancel_alarm_classic);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        id = getIntent().getExtras().getInt("id");
        context = this.getApplicationContext();

        seekBar = (SeekBar)findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(this);

        llParentClacssic = (LinearLayout)findViewById(R.id.ll_parent_classic);
        Util.getInstance(context).setTheme(llParentClacssic);

    }

    public void pressResetAlarmClassic(View view){

        int maxRepeat = getSharedPreferences(KEY_PREFERENCE, MODE_PRIVATE).getInt("maxRepeat", 15);
        TimeHelper.getInstance(context).currentRepeat =
                TimeHelper.getInstance(context).currentRepeat + 1;

        if(TimeHelper.getInstance(context).currentRepeat <= maxRepeat){
            Util.getInstance(context).resetAlarmUtil(id);
            this.finish();
        }
        else {
            Toast.makeText(context, getResources().getText(R.string.maxRepeatOverflow).toString(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.finish();
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        if(i < 10)
            continProgress = true;
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        continProgress = false;
    }

    @Override
    public void onStopTrackingTouch(final SeekBar seekBar) {

        final int progress = seekBar.getProgress();

        if(continProgress){
            if(progress >= 90){
                Util.getInstance(context).cancelAlarmUtil(id);
                this.finish();
            }
            else {
                seekBarRollBack(progress);
            }
        }
        else {
            seekBar.setProgress(0);
        }
    }

   private void seekBarRollBack(final int progress){

       if(t != null && t.isAlive())
           t.interrupt();

       t = new Thread(
               new Runnable() {
                   @Override
                   public void run() {
                       for (int i = 0;  i <= progress; i++) {

                           try {
                               successHandler.sendEmptyMessage(0);
                               t.sleep(1);
                           } catch (InterruptedException e) {
                               Log.e("My", "seek return fail");
                           }
                       }
                   }
               }
       );
       t.start();

   }

    Handler successHandler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            seekBar.setProgress(seekBar.getProgress() - 1);
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Util.getInstance(context).setTheme(llParentClacssic);
    }

}